package com.shms.action;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.shms.bean.GroupOfStudent;
import com.shms.constant.QueryConstant;
import com.shms.entity.ShStudent;
import com.shms.service.ShStudentService;

/**
 * 文件名：StudentAction.java
 * 编写者：杨勇
 * 创建时间：2013-3-18
 * 简要描述：
 * 
 */
public class StudentAction extends ActionSupport {
	
	private static final long serialVersionUID = 7610288363861827537L;
	private ShStudent shStudent;
	private ShStudentService shStudentService;
	private GroupOfStudent groupOfStudent;
	private String message;
	private Integer[] nos;
	
	public String addStudent() {
		//
		ActionContext.getContext().put("buildbno", 0);
		//
		ActionContext.getContext().put("homehno", 0);
		//
		ActionContext.getContext().put("classcname", 0);
		return SUCCESS;
	}
	
	/**
	 * 保存学生信息
	 * @return
	 */
	public String saveStudent() {
		boolean rtn = true;
		if (!shStudent.getPark1().equals(QueryConstant.BZ_UPDATE)) {
			boolean check = shStudentService.checkStudent(shStudent);
			//当check为true时说明该学号的学生信息已经存在
			if (check == true) {
				message = "已存在该学号的学生信息";
			} else {
				rtn = shStudentService.saveStudent(shStudent);
				if (rtn == false) {
					message = "保存失败";
				} else {
					message = "保存成功";
					//借用字段Park1，设置编辑数据的标志UPDATE
					shStudent.setPark1(QueryConstant.BZ_UPDATE);
					ActionContext.getContext().put("type", 0);
				}
			}
		} else {
			//将字段Park1的编辑标志置空
			shStudent.setPark1("");
			rtn = shStudentService.updateStudent(shStudent);
			if (rtn == false) {
				message = "保存失败";
			} else {
				message = "保存成功";
				ActionContext.getContext().put("type", 0);
			}
		}
		//返回楼号id
		ActionContext.getContext().put("buildbno", shStudent.getBid());
		//返回宿舍id
		ActionContext.getContext().put("homehno", shStudent.getHid());
		//返回班级id
		ActionContext.getContext().put("classcname", shStudent.getCid());
		return SUCCESS;
	}
	
	public String queryByStudentNo() {
		//获取要查询的单条记录no
		String no = ServletActionContext.getRequest().getParameter("no");
		ShStudent shStudentParams = new ShStudent();
		shStudentParams.setSno(no);
		shStudent = shStudentService.queryByNo(shStudentParams);
		//借用字段Park1，设置编辑数据的标志UPDATE
		shStudent.setPark1(QueryConstant.BZ_UPDATE);
		//
		ActionContext.getContext().put("buildbno", shStudent.getBid());
		//
		ActionContext.getContext().put("homehno", shStudent.getHid());
		//
		ActionContext.getContext().put("classcname", shStudent.getCid());
		//
		ActionContext.getContext().put("type", 0);
		
		return SUCCESS;
	}
	
	/**
	 * 根据分页信息，查询学生信息列表
	 * @return 
	 */
	public String queryStudent() {
		//获取传来的当前页数
		Integer currentPage = Integer.parseInt(ServletActionContext.getRequest().getParameter("page"));
		//获取传来的上一页或下一页标志
		String type = ServletActionContext.getRequest().getParameter("param");
		//构造查询学生信息的分页信息输入参数
		GroupOfStudent groupOfStudentParams = new GroupOfStudent();
		groupOfStudentParams.setCurrentPage(currentPage);
		groupOfStudentParams.setType(type);
		groupOfStudentParams.setPageSize(QueryConstant.F_Pagesize);
		//查询
		groupOfStudent = shStudentService.queryStudent(groupOfStudentParams);
		if (groupOfStudent.getShStudent() == null)
			return INPUT;
		else 
			return SUCCESS;
	}

	/**
	 * 批量删除学生信息
	 * @return 
	 */
	public String deleteStudent() {
		//获取要删除的单条记录id
		if (ServletActionContext.getRequest().getParameter("no") != null ) {
			Integer id = Integer.parseInt(ServletActionContext.getRequest().getParameter("no"));
			Integer[] ids = {id};
			nos = ids;
		}
		//删除成功重新查询数据，并返回
		if(shStudentService.batchDelete(nos)){
			//获取传来的当前页数
			Integer currentPage = groupOfStudent.getCurrentPage() - 1;
			//获取传来的上一页或下一页标志
			String type = groupOfStudent.getType();
			//构造查询动态信息的分页信息输入参数
			GroupOfStudent groupOfStudentParams = new GroupOfStudent();
			groupOfStudentParams.setCurrentPage(currentPage);
			groupOfStudentParams.setType(type);
			groupOfStudentParams.setPageSize(QueryConstant.F_Pagesize);
			//查询
			groupOfStudent = shStudentService.queryStudent(groupOfStudentParams);
			return SUCCESS;
		}else{
			message = "删除动态信息失败";
			return INPUT;
		}
	}
	
	public void setShStudentService(ShStudentService shStudentService) {
		this.shStudentService = shStudentService;
	}

	public ShStudentService getShStudentService() {
		return shStudentService;
	}

	public GroupOfStudent getGroupOfStudent() {
		return groupOfStudent;
	}

	public void setGroupOfStudent(GroupOfStudent groupOfStudent) {
		this.groupOfStudent = groupOfStudent;
	}

	public Integer[] getNos() {
		return nos;
	}

	public void setNos(Integer[] nos) {
		this.nos = nos;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public ShStudent getShStudent() {
		return shStudent;
	}

	public void setShStudent(ShStudent shStudent) {
		this.shStudent = shStudent;
	}
}
